package com.example.excelStudy;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;

import java.io.ByteArrayOutputStream;
import java.util.List;

/**
 * 生成文件并上传
 *
 * @author brad zhang
 * @version 2.0
 * @date 2021/10/27 11:21
 */
public class GenerateFileUpload {

    public static String createExcel(Class<?> obj, List<?> list, String sheetName){
        ByteArrayOutputStream bos = new ByteArrayOutputStream();//字节流
        EasyExcel.write(bos, obj).registerWriteHandler(excelStyleSet()).sheet(sheetName).doWrite(list);
        //调用上传方法
        // String resultUrl = qiniuClient.upload(bos.toByteArray());
        // System.out.println("水印文件上传七牛云成功返货resultUrl={}"+resultUrl);
        return "url";
    }
    /**
     * 默认的样式设置
     *
     * @return 样式信息
     */
    public static HorizontalCellStyleStrategy excelStyleSet() {
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 12);
        headWriteCellStyle.setWriteFont(headWriteFont);
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
        contentWriteFont.setFontHeightInPoints((short) 10);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }
}
